home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / tsnr21.zip / CIPHER.C next >
Text File  |  1992-11-16  |  7KB  |  207 lines

  1. /*
  2.     Program Name:   Cipher.C
  3.     Author:         Curtis Little
  4.  
  5.     Copyright (c) 1990, By Curtis Little
  6.     ------------------------- ALL RIGHTS RESERVED ------------------------
  7.     Created:  10/31/1990 at 10:06 am
  8.  
  9.     Compiler: ------------------------------------------------------------
  10.     Turbo C 2.0
  11.  
  12.     Description: ---------------------------------------------------------
  13.     This program is a tool that automates the creation of tables to
  14.     perform file encryption/decryption with.  This is a sample program
  15.     distributed with Turbo SNR to help generate tables that demonstrate
  16.     the use of multiple contexts.
  17.  
  18.     A list of substitution strings will be listed to standard out (which
  19.     can be redirected to any file you desire) that can be used both to
  20.     encrypt a file and decrypt a file.  When you redirect output to a file
  21.     you will need to go in the file and manually write out the decryption
  22.     table portion of the file.  (You can search for the word 'Decryption'
  23.     to locate the start of the decryption table portion).
  24.  
  25.     The file created has the following format:
  26.  
  27.         Header to indicate the start of the encryption portion of strings
  28.         Encryption strings
  29.         Header to indicate the start of the decryption portion of strings
  30.         Decryption strings
  31.  
  32.     You can specify how many context settings to use (1-10) by including
  33.     a parameter on the command line.  For example to use 4 contexts and
  34.     direct output to a file called MYFILE.TAB you would use the following
  35.     command line:
  36.  
  37.         cipher 4 > myfile.tab
  38.  
  39.     At default CIPHER uses three contexts when you don't specify the
  40.     number.
  41.  
  42.     Modification History: ------------------------------------------------
  43.  
  44.     Revision:  1.0  Last Revised:  10/31/1990 at 10:06 by CBL
  45.     Description:  Original Creation.
  46.     ----------------------------------------------------------------------
  47. */
  48.  
  49.  
  50. #include <stdio.h>
  51. #include <time.h>
  52. #include <stdlib.h>
  53.  
  54. #define MAX_CONTEXT 10      /* maximum number of contexts allowd */
  55. #define MAX_TRIES   256     /* maximum attempts to try to get a unique */
  56.                             /* random number before using a linear search */
  57. #define MAX_BYTE_VAL 255    /* maximum number a single character can hold */
  58.  
  59.  
  60. int *uses[MAX_CONTEXT];     /* pointers to the table entries we used so far */
  61.  
  62.  
  63. /*
  64.     findone()
  65.  
  66.     This function uses random numbers to pick the character to replace
  67.     with.  The original character we're replacing is put in the uses[x]
  68.     array so we know which characters we transposed.
  69.  
  70.     Returns:    The character to substitute with.
  71. */
  72. int findone( int use[], int c )
  73. {
  74.     register int i, j=0;
  75.  
  76.     /* loop to try and generate a random number that hasn't already been */
  77.     /* used in the current context */
  78.     while (j++ < MAX_TRIES) {
  79.         i=random(256);
  80.         if (use[i]==-1) {            /* is the character already used? */
  81.             use[i] = c;             /* if not we'll use it now */
  82.             return(i);
  83.         }
  84.     }
  85.  
  86.     /* search up to the end character now... */
  87.     while (++i < 256) {
  88.         if (use[i]==-1) {
  89.             use[i]=c;
  90.             return(i);
  91.         }
  92.     }
  93.  
  94.     /* search down now... */
  95.     while (i-- > 0)
  96.         if (use[i]==-1) {
  97.             use[i]=c;
  98.             return(i);
  99.         }
  100.  
  101.     /* we have an internal error if execution gets here */
  102.     printf( "\n\a\aError:  Can't map to a unique character!  Program halted.\n\n" );
  103.     exit(1);
  104. }
  105.  
  106. /*
  107.     fixchar()
  108.  
  109.     This function creates a string out of the specified character.  This
  110.     is used so we can add the leading backslash for special characters as
  111.     well as support the NULL character.
  112.  
  113.     The buffer must be at least 5 bytes long.
  114.  
  115.     Returns:  None.
  116. */
  117. void fixchar( char *buf, int c )
  118. {
  119.  
  120.     buf[1]=0;               /* set up the most common case */
  121.  
  122.     switch (c) {
  123.     case ':': case ';': case '\\': case '=': case '"':
  124.         sprintf( buf, "\\%c", c );
  125.         break;
  126.     default:
  127.         if (c < 32 || c > 126)
  128.             sprintf( buf, "\\%d", c );
  129.         else
  130.             buf[0] = c;
  131.     }
  132. }
  133.  
  134. void usage( char *message )
  135. {
  136.     fprintf( stderr, "Error:  %s\n", message );
  137.     fprintf( stderr, "Calling Syntax: CIPHER [num_contexts]\n" );
  138.     exit(1);
  139. }
  140. int main( int argc, char *argv[] )
  141. {
  142.     register int i, j;
  143.     int k, l;
  144.     int contexts=3;           /* default number of contexts */
  145.     char buf[5];
  146.     char buf1[5];
  147.     extern void *malloc( unsigned size );
  148.  
  149.     fprintf( stderr, "CIPHER - Copyright (C)1990, By Curtis Little   All Rights Reserved.\n" );
  150.     fprintf( stderr, "Create CIPHER/DECIPHER tables for use by Turbo SNR.\n\n" );
  151.  
  152.     /* make sure the command line looks ok. */
  153.     if (argc > 2) {
  154.         usage( "Too many parameters specified!" );
  155.     }
  156.  
  157.     if (argc == 2) {
  158.         contexts = atoi(argv[1]);
  159.         if (contexts < 1 || contexts > MAX_CONTEXT)
  160.             usage( "Invalid number of contexts specified!" );
  161.     }
  162.  
  163.     /* seed the random number generator using the system clock */
  164.     randomize();
  165.  
  166.     /* loop to allocate the required array for each context */
  167.     for (i=0; i < contexts; i++) {
  168.         if ((uses[i] = (int *) malloc(sizeof(int)*(MAX_BYTE_VAL+1)))==NULL)
  169.             usage( "Not enough memory!" );
  170.     }
  171.  
  172.     /* now loop to initialize all the table elements */
  173.     for (i=0; i < contexts; i++) {
  174.         for (j=0; j < MAX_BYTE_VAL + 1; j++)
  175.             uses[i][j] = -1;
  176.     }
  177.  
  178.     /* now output the encryption table */
  179.     printf( ";\n; File Encryption Table for Turbo SNR.\n" );
  180.     printf(    "; ------------------------------------\n;\n\n\n" );
  181.  
  182.     /* now loop to create the tables as required */
  183.     for (i=0; i < contexts; i++ ) {
  184.         for (j=0; j < MAX_BYTE_VAL + 1; j++) {
  185.             fixchar(buf, j);
  186.             fixchar( buf1, findone(uses[i], j));
  187.             printf( "\"%d:%s=%d:%s\"\n", i, buf,
  188.                 i == contexts - 1 ? 0 : i + 1, buf1 );
  189.         }
  190.     }
  191.  
  192.     /* now output the "cure" */
  193.     printf( "\n\n;-------------------------------------------------------------\n" );
  194.     printf( ";\n; File Decryption Table for Turbo SNR.\n" );
  195.     printf(    "; ------------------------------------\n;\n\n\n" );
  196.  
  197.     for (i=0; i < contexts; i++ ) {
  198.         for (j=0; j < MAX_BYTE_VAL + 1; j++) {
  199.             fixchar( buf, j );
  200.             fixchar( buf1, uses[i][j] );
  201.             printf( "\"%d:%s=%d:%s\"\n", i, buf, 
  202.                 i == contexts - 1 ? 0 : i + 1, buf1 );
  203.         }
  204.     }
  205.     return(0);
  206. }
  207.